package com.itqf.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.InputStream;

/**
 * projectName: springframeworkgp8
 *
 * @author: 赵伟风
 * time: 2021/8/2 16:27
 * description:
 */
public class SqlSessionFactoryFactory implements FactoryBean<SqlSessionFactory> {



    private String config;

    public void setConfig(String config) {
        this.config = config;
    }

    /**
     * 返回你要制造的对象
     * @return
     * @throws Exception
     */
    @Override
    public SqlSessionFactory getObject() throws Exception {

        InputStream resourceAsStream = Resources.getResourceAsStream(config);

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);

        return sqlSessionFactory;
    }


    /**
     * 制造对象的类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return SqlSessionFactory.class;
    }


    /**
     * 是否是单例
     * @return
     */
    @Override
    public boolean isSingleton() {
        return true;
    }
}
